home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / recio212.zip / rputs.c < prev    next >
C/C++ Source or Header  |  1995-01-29  |  4KB  |  108 lines

  1. /*****************************************************************************
  2.    MODULE: rputs.c
  3.   PURPOSE: recio character delimited string and char output functions
  4. COPYRIGHT: (C) 1994-1995, William Pierpoint
  5.  COMPILER: Borland C Version 3.1
  6.        OS: MSDOS Version 6.2
  7.   VERSION: 2.12
  8.   RELEASE: January 29, 1995
  9. *****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #include "recio.h"
  16.  
  17. extern int _risready(REC *rp, int mode);
  18. extern int _rputc(REC *rp, int ch);
  19.  
  20. #define rflags(rp)       ((rp)->r_flags)
  21. #define rfp(rp)          ((rp)->r_fp)
  22. #define rcol(rp)         ((rp)->r_colno)
  23. #define rfldch(rp)       ((rp)->r_fldch)
  24. #define rtxtch(rp)       ((rp)->r_txtch)
  25.  
  26. /****************************************************************************/
  27. int                          /* return 0 on success; !0 on error            */
  28.     _rputc(                  /* put character to record stream              */
  29.         REC *rp,             /* record pointer                              */
  30.         int  ch)             /* character to put to record stream           */
  31. /****************************************************************************/
  32. {
  33.     int err = fputc(ch, rfp(rp));
  34.     if (err==EOF) {
  35.         rseterr(rp, R_ENOPUT);
  36.     } else {
  37.         rcol(rp)++;
  38.         err = 0;
  39.     }
  40.     return err;
  41. }
  42.  
  43. /****************************************************************************/
  44. void                         /* returns nothing                             */
  45.     rputc(                   /* put character to record stream              */
  46.         REC *rp,             /* record pointer                              */
  47.         int  ch)             /* character to put to record stream           */
  48. /****************************************************************************/
  49. {
  50.     if (_risready(rp, R_WRITE)) {
  51.         rfldno(rp)++;
  52.         rflags(rp) &= ~_R_TXT;
  53.  
  54.         /* if not first field, put field separator if not null */
  55.         if (rfldno(rp) > 1 && rfldch(rp)) {
  56.             if (_rputc(rp, rfldch(rp))) goto done;
  57.         }
  58.  
  59.         /* put character */
  60.         _rputc(rp, ch);
  61.     }
  62. done:
  63. }
  64.  
  65. /****************************************************************************/
  66. void                         /* returns nothing                             */
  67.     rputs(                   /* put string to record stream                 */
  68.         REC  *rp,            /* record pointer                              */
  69.   const char *str)           /* pointer to string                           */
  70. /****************************************************************************/
  71. /* note: assumes str does not contain a newline character */
  72. {
  73.     size_t sl;               /* string length */
  74.  
  75.     if (_risready(rp, R_WRITE)) {
  76.         rfldno(rp)++;
  77.         rflags(rp) &= ~_R_TXT;
  78.  
  79.         /* if not first field, put field separator if not null */
  80.         if (rfldno(rp) > 1 && rfldch(rp)) {
  81.             if (_rputc(rp, rfldch(rp))) goto done;
  82.         }
  83.  
  84.         /* if text delimiter not space or null, put leading text delimiter */
  85.         if (!(rtxtch(rp)==' ' || rtxtch(rp)=='\0')) {
  86.             if (_rputc(rp, rtxtch(rp))) {
  87.                 rflags(rp) |= _R_TXT;
  88.                 goto done;
  89.             }
  90.         }
  91.  
  92.         /* put string */
  93.         sl = strlen(str);
  94.         if (fputs(str, rfp(rp)) != EOF) { 
  95.             rcol(rp) += sl;
  96.         } else {
  97.             rseterr(rp, R_ENOPUT);
  98.             goto done;
  99.         }
  100.  
  101.         /* if text delimiter not space or null, put trailing text delimiter */
  102.         if (!(rtxtch(rp)==' ' || rtxtch(rp)=='\0')) {
  103.             _rputc(rp, rtxtch(rp));
  104.         }
  105.     }
  106. done:
  107. }
  108.